package com.hzbank.tcps.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzbank.tcps.entity.EntBase;
import com.hzbank.tcps.entity.EntNewInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;

@Api(value = "/sync", tags = "数据同步")
@RestController
@RequestMapping("/sync")
@Slf4j
public class MonitorController {

	@ApiOperation(value = "企业名单同步接口", notes = "企业名单同步接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "管理流水ID", required = true, dataType = "String", paramType = "path", defaultValue = "0"),
			@ApiImplicitParam(name = "entBases", // 参数名
					value = "企业基本信息", required = true, // 参数是否必须传
					allowMultiple = true, // 是否允许重复
					dataType = "EntBase", // 参数类型
					paramType = "body" // 参数放在哪个地方
			) })
	@RequestMapping(value = "list/{id}", method = RequestMethod.POST)
	public ResponseEntity<JSONObject> update(@PathVariable("id") String id, @RequestBody List<EntBase> entBases) {
		JSONObject jsonObject = new JSONObject();
		// to do
		log.info("请求体 {}", entBases);
		return ResponseEntity.ok(jsonObject);
	}

//		@DatetimeFormat是将String转换成Date，一般前台给后台传值时用
//		@JsonFormat(pattern="yyyy-MM-dd")  将Date转换成String  一般后台传值给前台时
	@ApiOperation("数据定时获取接口")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "string",format="date", 
				paramType = "path" ,example="yyyy-MM-dd HH:mm:ss" ,defaultValue="2018-07-17 07:00:00"),
		@ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "string", format="date",
				paramType = "path" ,example="yyyy-MM-dd HH:mm:ss",defaultValue="2018-07-17 08:00:00") })
	@RequestMapping(value = "info/{startTime}/{endTime}", method = RequestMethod.POST)
//	@ApiResponse(response="Object")
	@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
//	  public ResponseEntity<String> placeOrder(
	public @ResponseBody ArrayList<EntNewInfo>  info(
			@PathVariable("startTime") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  Date startTime,
			@PathVariable("endTime") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date endTime) {
		log.info("数据定时获取接口 开始时间:{} 结束时间:{}",startTime,endTime);
		EntNewInfo entNewInfo=new EntNewInfo();
		entNewInfo.setGetTime(new Date());
		entNewInfo.setKeyID(1111);
		entNewInfo.setNewID(11111232);
		entNewInfo.setPhotoId(122523465);
		entNewInfo.setTitle("银行股表现抢眼 杭州银行涨逾3%");
		entNewInfo.setTextbody("中国证券网讯 31日早盘，银行股集体拉升，截至发稿，银行板块全部飘红，杭州银行、吴江银行、张家港行涨逾3%，贵阳银行、上海银行涨逾2%，农业银行、平安银行等个股纷纷拉升。");
		entNewInfo.setUrl("http://wemedia.ifeng.com/17206951/wemedia.shtml");
		entNewInfo.setWebsite("http://baidu.com");
		EntNewInfo entNewInfo1=new EntNewInfo();
		entNewInfo1.setGetTime(new Date());
		entNewInfo1.setKeyID(11111);
		entNewInfo1.setNewID(111112232);
		entNewInfo1.setPhotoId(1225233465);
		entNewInfo1.setTitle("今世缘两董事拟合计减持不超0.6%股份");
		entNewInfo1.setTextbody("中国证券网讯（记者 孔子元）今世缘公告，公司董事王卫东计划自2018年8月10日至2019年2月5日，减持合计不超过500万股，减持比例不超过公司总股本的0.399%；公司董事倪从春计划自2019年8月10日至2019年2月5日，通减持合计不超过250万股，减持比例不超过公司总股本的0.199%。");
		entNewInfo1.setUrl("http://wemedia.ifeng.com/69590619/wemedia.shtml");
		entNewInfo1.setWebsite("http://baidu.com");
		ArrayList<EntNewInfo> entNewInfos=new ArrayList<>();
		entNewInfos.add(entNewInfo);
		entNewInfos.add(entNewInfo1);
		return entNewInfos;
	}

	@ApiOperation(value = "获取截图", notes = "根据id获取截图")
	@ApiImplicitParam(name = "id", value = "照片ID", required = true, dataType = "int", paramType = "path")
	@RequestMapping(value = "photo/{id}", method = RequestMethod.GET)
	    public ResponseEntity<byte[]> getPhoto(@PathVariable("id") Integer id) throws IOException {
	        FileSystemResource file = new FileSystemResource("/home/ubuntu/图片/1.png");

	        byte [] content = new byte[(int)file.contentLength()];
	        IOUtils.read(file.getInputStream(), content);

	        return ResponseEntity.ok()
	            .contentType(MediaType.IMAGE_PNG)
	            .lastModified(file.lastModified())
	            .contentLength(file.contentLength())
	            .body(content);
	    }
}
